/**
 *  test15.c - Test about EEPROM device with IIC interface.
 *             It runs on ARMulator or ARM7TDMI CPU.
 *
 *  Copyright (C) 2008-2010 ZhangHu
 *  All rights reserved.
 *  E-MAIL: anmnmnly@gmail.com
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */
#include "include/core.h"
#include "drivers/uart_console.h"
#include "drivers/chdev.h"
#include "devs.h"

#define BUF_LEN 32
#define STACK_SIZE 4096


static void WriteTask(void)
{
    char_t wbuf[BUF_LEN] = "Welcome to use Bycore!";
    chdev_t *dev = NULL;

    while(1) {
        dev = CDevOpen(IIC_EEPROM);
        if(dev == NULL) {
            Print("WriteTask: Cann't open IIC_EEPROM!\n");
            osWait(100);
            continue;
        }

        Print("WriteTask: %s\n", wbuf);
        CDevWrite(dev, wbuf, sizeof(wbuf));
        CDevClose(dev);

        osWait(100);
    }
}



static void ReadTask(void)
{
    char_t rbuf[BUF_LEN];
    chdev_t *dev = NULL;

    while(1) {
        kmemset(rbuf, 0, sizeof(rbuf));
        dev = CDevOpen(IIC_EEPROM);
        if(dev == NULL) {
            Print("ReadTask: Cann't open IIC_EEPROM!\n");
            osWait(100);
            continue;
        }

        CDevRead(dev, rbuf, sizeof(rbuf));
        CDevClose(dev);

        Print("ReadTask: %s\n", rbuf);
        osWait(100);
    }
}



void Main(void)
{
    osInitTask(WriteTask, 100, 0, 5, STACK_SIZE);
    osInitTask(ReadTask, 101, 1, 5, STACK_SIZE);
}
